package com.mycjj.android.activity;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.bean.MesBean;
import com.mycjj.android.bean.ReservationEntity;
import com.mycjj.android.bean.ReservationGetBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class YuyueDetailActivity extends BaseActivity{
	/*
	 * 默认下面 显示的打分 和 评价
	 */ 
	private LinearLayout ll_type1;
	private LinearLayout ll_type2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_me_yuyue_detail);
		init();
		initData();
	}
	
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private String id;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("预约详情");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				YuyueDetailActivity.this.finish();
			}
		});
	}
	
	
	private TextView tv_yy_type1,tv_yy_type2,tv_yy_type3,tv_yy_type4,
					tv_yy_type5,tv_yy_type6,tv_yy_type8;
	private RatingBar ratingBar1,ratingBar2; 
	private ImageView iv_duan_xin,iv_dian_hua,iv_yy_clear;
	private EditText et_yy_type1;
	private TextView tv_yuyue_pj;
	private void init(){
		
		Intent intent = getIntent();
		if(intent != null){
			id = intent.getStringExtra("id");
		}
		initHeader();
		ll_type1 = (LinearLayout)findViewById(R.id.ll_type1);
		ll_type2 = (LinearLayout)findViewById(R.id.ll_type2);
		initView();
	}
	
	private void initView(){
		tv_yy_type1 = (TextView)findViewById(R.id.tv_yy_type1);
		tv_yy_type2 = (TextView)findViewById(R.id.tv_yy_type2);
		tv_yy_type3 = (TextView)findViewById(R.id.tv_yy_type3);
		tv_yy_type4 = (TextView)findViewById(R.id.tv_yy_type4);
		tv_yy_type5 = (TextView)findViewById(R.id.tv_yy_type5);
		tv_yy_type6 = (TextView)findViewById(R.id.tv_yy_type6);
		tv_yy_type8 = (TextView)findViewById(R.id.tv_yy_type8);
		
		ratingBar1 = (RatingBar)findViewById(R.id.ratingBar1);
		ratingBar2 = (RatingBar)findViewById(R.id.ratingBar2);
		
		iv_duan_xin = (ImageView)findViewById(R.id.iv_duan_xin);
		iv_dian_hua = (ImageView)findViewById(R.id.iv_dian_hua);
		iv_yy_clear = (ImageView)findViewById(R.id.iv_yy_clear);
		
		et_yy_type1 = (EditText)findViewById(R.id.et_yy_type1);
		tv_yuyue_pj = (TextView)findViewById(R.id.tv_yuyue_pj);
		
		tv_yuyue_pj.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				yuyuePj();
			}
		});
	}
	
	private void initDataToView(final ReservationEntity entity){
		if(entity.getType()==1){//[1-维修 | 2-保养]
			tv_yy_type1.setText("维修预约");
		}else{
			tv_yy_type1.setText("保养预约");
		}
		tv_yy_type2.setText(entity.getStore_name());//店名
		tv_yy_type3.setText(entity.getArrive_date()+ " " +entity.getArrive_time());//到店日期 和时间
		
		tv_yy_type4.setText(entity.getProject());//项目名称
		tv_yy_type5.setText(entity.getAdviser_name());//人名
		tv_yy_type6.setText("-"+entity.getPrice());//消费金额
		tv_yy_type8.setText(entity.getEvaluate_msg());//评价
		ratingBar2.setRating((float)entity.getEvaluate_level());
		iv_yy_clear.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				clearYuyue();
			}
		});
		
		
		iv_duan_xin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!TextUtils.isEmpty(entity.getMobile())){
					Uri smsToUri = Uri.parse("smsto:"); 
					Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri); 
					sendIntent.putExtra("address", entity.getMobile());   //电话号码，这行去掉的话，默认就没有电话
					sendIntent.putExtra("sms_body",""); 
					sendIntent.setType("vnd.android-dir/mms-sms"); 
					startActivity(sendIntent);
				}else{
					Toast.makeText(YuyueDetailActivity.this, "没有获取到手机号码！", Toast.LENGTH_SHORT).show();
				}
			}
		});
			
		iv_dian_hua.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!TextUtils.isEmpty(entity.getMobile())){
					AlertDialog.Builder bu = new AlertDialog.Builder(YuyueDetailActivity.this);
					LayoutInflater layinInflater = LayoutInflater.from(YuyueDetailActivity.this);
					View viewphone = layinInflater.inflate(R.layout.store_show_phone_dialog, null);
					TextView determine = (TextView) viewphone.findViewById(R.id.determine);
					TextView showPhone = (TextView) viewphone.findViewById(R.id.showphone);
	
					determine.setText("确定");
					showPhone.setText(entity.getMobile());
					bu.setView(viewphone);
					bu.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int whitch) {
							Intent in = new Intent();
							in.setAction(Intent.ACTION_CALL);
							Uri uri = Uri.parse("tel:" + entity.getMobile());
							in.setData(uri);
							startActivity(in);
						}
					});
					bu.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int whitch) {
						}
					});
					bu.show();
				}else{
					Toast.makeText(YuyueDetailActivity.this, "没有获取到手机号码！", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	private void clearYuyue(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("id", id);
		request.requestReservationCancelWithDoneHandler(null, params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "---content=="+content);
				ObjectMapper mapper = new ObjectMapper();
				try {
					MesBean mesBean = mapper.readValue(content, MesBean.class);
					if(mesBean.getCode() == 200){
						YuyueDetailActivity.this.finish();
					}else{
						Toast.makeText(YuyueDetailActivity.this, "取消预约失败!", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Toast.makeText(YuyueDetailActivity.this, "请求数据错误!", Toast.LENGTH_LONG).show();
			}
		});
	}
	
	private void yuyuePj(){
		String mes = et_yy_type1.getText().toString();
		if(TextUtils.isEmpty(mes)){
			Toast.makeText(this, "请输入评价内容", Toast.LENGTH_SHORT).show();
			return;
		}
		float rating = ratingBar1.getRating();
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("id", id);
		params.put("level", Float.toString(rating));
		params.put("msg", mes);
		
		request.requestReservationEvaluateWithDoneHandler(null, params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "---content=="+content);
				ObjectMapper mapper = new ObjectMapper();
				try {
					MesBean mesBean = mapper.readValue(content, MesBean.class);
					if(mesBean.getCode() == 200){
						ll_type1.setVisibility(View.VISIBLE);
						ll_type2.setVisibility(View.GONE);
						iv_yy_clear.setVisibility(View.GONE);
					}else{
						Toast.makeText(YuyueDetailActivity.this, "评论失败!", Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Toast.makeText(YuyueDetailActivity.this, "请求数据错误!", Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
	private void initData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("id", id);
		request.requestReservationGetWithDoneHandler("",params,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				ReservationGetBaseBean basebean = ProjectDataManage.getInstance().getReservationGetBaseBean();
				if(basebean != null){
					Log.e("mark","bean:"+basebean.toString());
					if(basebean.getCode() == 200){
						ReservationEntity entity = basebean.getData();
						if(entity != null){
							if(entity.getStatus()==0){ //未受理
								ll_type1.setVisibility(View.VISIBLE);
								ll_type2.setVisibility(View.GONE);
								iv_yy_clear.setVisibility(View.VISIBLE);
							}else if(entity.getStatus() ==1){//已受理
								ll_type1.setVisibility(View.VISIBLE);
								ll_type2.setVisibility(View.GONE);
								iv_yy_clear.setVisibility(View.GONE);
							}else if(entity.getStatus() ==2){//待评价
								ll_type1.setVisibility(View.GONE);
								ll_type2.setVisibility(View.VISIBLE);
								iv_yy_clear.setVisibility(View.GONE);
							}else if(entity.getStatus() == 3){//已完成
								ll_type1.setVisibility(View.VISIBLE);
								ll_type2.setVisibility(View.GONE);
								iv_yy_clear.setVisibility(View.GONE);
							}
							if(entity.getIf_cancel() == 1){
								iv_yy_clear.setVisibility(View.GONE);
							}
							
							initDataToView(entity);
							
						}else{
							Log.e("mark","ReservationEntity is null");
						}
					}else{
						Toast.makeText(YuyueDetailActivity.this, basebean.getMsg(), Toast.LENGTH_LONG).show();
					}
				}else {
					Toast.makeText(YuyueDetailActivity.this, "basebean is empty!", Toast.LENGTH_LONG).show();
					Log.e("mark","bean is empty");
				}
			}
		});
	}
	
}
